#include "scandev.h"
#include "QtGui"
#include "time.h"
#include "unistd.h"
#include <QDebug>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <termios.h>
#include <QTextCodec>
#include <QDebug>
#include <QObject>
scanDev::scanDev(int comId)
{
    this->fd = comId;
}
//数据接收处理

bool scanDev::generalDevRecProcess(int)
{
    char buf[128];
    char data[30];
    int res;
    usleep(20000);
    res = ::read(fd,buf,sizeof(buf));
    if(res)
    {
        if((buf[4] != 0x83)&&(buf[0] == 0xAA))   //有卡
        {
        bool oA;
        memset(data,0,sizeof(data));
        sprintf(data,"%02X%02X%02X%02X",buf[8],buf[7],buf[6],buf[5]);
        QString ic_num = QString::number(QString(data).toULong(&oA,16));
        qDebug()<<"data:ic"<<ic_num;
        this->strId=ic_num;
        return true;
        }else{
         return false;
        }
    }else{
    return false;
    }
}


bool scanDev::sendCard(QString num)
{
    char r_cmd[6];
    bool ok;
    char bit;
    r_cmd[0]=0xAA;
    r_cmd[1]=0x01;
    r_cmd[2]=0x02;
    r_cmd[3]=0xEE;
    r_cmd[4]=num.toInt(&ok,16);
    bit=r_cmd[2];
    for(int i=3;i<5;i++)
    {
        bit=bit^r_cmd[i];
    }
    r_cmd[5]=bit;
    tcflush(this->fd,TCIOFLUSH);
    ::write(this->fd,r_cmd,6);
    return true;
}


void scanDev::print_pound_messgae() //打印磅单信息
{
    char *buf1;
    char cmd[3];
    char buf[100];
    QByteArray ba;
    QString str1;
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
    memset(cmd,0,sizeof(cmd));   //流水号
    cmd[0]=0x0A;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("流水号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("流水号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));

    memset(cmd,0,sizeof(cmd));   //车号
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("车号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));
    //printf("buf is %s\n",buf);

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("车号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));
    // printf("buf is %s\n",buf);

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("车号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));
    //printf("buf is %s\n",buf);

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);
    str1 = QString::fromLocal8Bit("车号:");
    str1 = str1 + QString("%1").arg(QString("123"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));
    //printf("buf is %s\n",buf);

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0D;
    cmd[1]=0x1B;
    cmd[2]=0x38;
    cmd[3]=0x04;
    ::write(this->fd,cmd,4);

    str1 = QString::fromLocal8Bit("日期:");
    str1 = str1 + QString("%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:ss"));
    ba=str1.toAscii();
    buf1=ba.data();
    memset(buf,0,sizeof(buf));
    strcat(buf,buf1);
    ::write(this->fd,buf,sizeof(buf));
    //printf("buf is %s\n",buf);

    memset(cmd,0,sizeof(cmd));
    cmd[0]=0x0A;
    ::write(this->fd,cmd,1);
    ::write(this->fd,cmd,1);
    ::write(this->fd,cmd,1);
    ::write(this->fd,cmd,1);
    ::write(this->fd,cmd,1);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
}
